package com.darrenchan.leetcode;

import java.util.Arrays;

/**
 * @Desc
 * @Author chenchi03
 * @CreateTime 2020-09-12 21:43
 */
public class Metrix90 {
    public static void main(String[] args) {
        int[][] a = new int[][]{{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};
        int i = 0, j = 0, i2 = a.length - 1, j2 = a[0].length - 1;
        while (i < i2 && j < j2) {
            rotate(a, i++, j++, i2--, j2--);
        }

        for (int k = 0; k < a.length; k++) {
            for (int l = 0; l < a[0].length; l++) {
                System.out.print(a[k][l] + " ");
            }
            System.out.println();
        }
    }

    public static void rotate(int[][] a, int i, int j, int i2, int j2) {
        int times = i2 - i;
        int temp = 0;
        for (int k = 0; k < times; k++) {
            temp = a[i][j + k];
            a[i][j + k] = a[i2 - k][j];
            a[i2 - k][j] = a[i2][j2 - k];
            a[i2][j2 - k] = a[i + k][j2];
            a[i + k][j2] = temp;
        }
    }
}
